package cn.com.io.day20151211;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Created by Administrator on 2015/12/11.
 */
public class RandomAccessFileTest {
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(System.getProperty("user.dir")+"\\src\\cn\\com\\io\\day20151211\\RandomAccessFileTest.java","r");//以只读方式打开一个RandomAccessFile对象
            System.out.println("RandomAccessFile的文件指针的初始位置："+raf.getFilePointer());//获取RandomAccessFile对象文件指针的位置，初始位置是0
            raf.seek(300);//移动文件记录指针的位置
            byte[] buf = new byte[1024];
            int hasRead = 0;//用于保存实际读取的字节数
            while((hasRead = raf.read(buf)) >0){
                System.out.println(new String(buf , 0 ,hasRead));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(raf !=null){
                raf.close();
            }
        }

    }
}
